home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Fudd source Folder / Fudd ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-07  |  6.5 KB  |  296 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Fudd -=- convert text to Elmer Fudd talk
  9. Copyright ©1994, Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg about.h"
  32. #include "msg help.h"
  33. #include "msg menus.h"
  34. #include "msg prefs.h"
  35. #include "msg environment.h"
  36. #include "fudd.h"
  37. #include "AppleEvents.h"
  38. #include "EPPC.h"
  39.  
  40. void main(void)
  41. {
  42.     MaxApplZone();    
  43.     InitGraf(&thePort);
  44.     InitFonts();
  45.     FlushEvents(everyEvent, 0);
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(0L);
  50.     InitCursor();
  51.     GetDateTime(&randSeed);
  52.     
  53.     CheckEnvironment();    
  54.     DoIntegrityCheck();    
  55.     InitMSGGraphics();
  56.     InitHelp();
  57.     PrefsError(PreferencesInit());
  58.     InitEnvironment();    
  59.     InitProgram();
  60.     EventLoop();    
  61.     ShutDownEnvironment();
  62.     ExitToShell();
  63. }
  64.  
  65. void EventLoop(void)
  66. {
  67.     EventRecord        theEvent;
  68.     int                i;
  69.     
  70.     while (!gDone)
  71.     {
  72.         SetCursor(&arrow);
  73.         HiliteMenu(0);
  74.         
  75.         if (FrontWindow()!=0L)
  76.         {
  77.             for (i=0; i<NUM_WINDOWS; i++)
  78.             {
  79.                 if (FrontWindow()==gTheWindow[i])
  80.                 {
  81.                     SetPort(gTheWindow[i]);
  82.                     i=NUM_WINDOWS;
  83.                 }
  84.             }
  85.         }
  86.             
  87.         if (WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? 100 :
  88.             (((gTheWindow[kAboutMSG]!=0L) && (FrontWindow()==gTheWindow[kAboutMSG])) ?
  89.             0 : 30), 0L))
  90.             DispatchEvents(theEvent);
  91.         
  92.         if ((!gIsInBackground) && (gTheWindow[kAboutMSG]!=0L) &&
  93.             (FrontWindow()==gTheWindow[kAboutMSG]))
  94.             DoTheMSGThing();
  95.     }
  96. }
  97.  
  98. void DispatchEvents(EventRecord theEvent)
  99. {
  100.     int                i;
  101.     OSErr            isHuman;
  102.     Point            thisPoint;
  103.     int                index;
  104.     char            thisChar;
  105.     unsigned long    dummy;
  106.     WindowPtr        theWindow;
  107.     
  108.     index=-1;
  109.     if ((theWindow=FrontWindow())!=0L)
  110.     {
  111.         for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  112.             if (theWindow==gTheWindow[i])
  113.                 index=i;
  114.     }
  115.     
  116.     switch (theEvent.what)
  117.     {
  118.         case nullEvent:
  119.             break;
  120.         case mouseDown:
  121.             HandleMouseDown(theEvent);
  122.             break;
  123.         case keyDown:
  124.         case autoKey:
  125.             thisChar=(char)(theEvent.message & charCodeMask);
  126.             if(theEvent.modifiers & cmdKey)
  127.             {
  128.                 AdjustMenus();
  129.                 HandleMenu(MenuKey(thisChar));
  130.             }
  131.             else
  132.                 switch (index)
  133.                 {
  134.                     case kAbout:
  135.                         CloseTheWindow(index);
  136.                         break;
  137.                     case kAboutMSG:
  138.                         UpdateTheWindow(index);
  139.                         Delay(30, &dummy);
  140.                         CloseTheWindow(index);
  141.                         break;
  142.                     case kHelp:
  143.                         HelpKeyEvent(thisChar);
  144.                         break;
  145.                 }
  146.             break;
  147.         case diskEvt:
  148.             if (HiWord(theEvent.message)!=noErr)
  149.             {
  150.                 DILoad();
  151.                 SetPt(&thisPoint, 120, 120);
  152.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  153.                 DIUnload();
  154.             }
  155.             break;
  156.         case updateEvt:
  157.             BeginUpdate((WindowPtr)theEvent.message);
  158.             
  159.             for (i=0; i<NUM_WINDOWS; i++)
  160.                 if ((WindowPtr)theEvent.message == gTheWindow[i])
  161.                     UpdateTheWindow(i);
  162.                     
  163.             EndUpdate((WindowPtr)theEvent.message);
  164.             break;
  165.         case activateEvt:
  166.             if (((WindowPtr)theEvent.message==gTheWindow[kAboutMSG]) &&
  167.                 ((theEvent.modifiers&activeFlag)==0))
  168.                 UpdateTheWindow(kAboutMSG);
  169.             break;
  170.         case osEvt:
  171.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  172.             {
  173.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  174.                 if ((gIsInBackground) && ((gTheWindow[kAboutMSG]!=0L) &&
  175.                     (FrontWindow()==gTheWindow[kAboutMSG])))
  176.                     UpdateTheWindow(kAboutMSG);
  177.             }
  178.             break;
  179.         case kHighLevelEvent:
  180.             if (gHasAppleEvents)
  181.                 AEProcessAppleEvent(&theEvent);
  182.             break;
  183.     }
  184. }
  185.  
  186. void HandleMouseDown(EventRecord theEvent)
  187. {
  188.     WindowPtr            theWindow;
  189.     int                    windowCode;
  190.     long                windSize;
  191.     GrafPtr                oldPort;
  192.     int                    i;
  193.     Rect                sizeRect;
  194.     Boolean                gotone;
  195.     int                    index;
  196.     unsigned long        dummy;
  197.     
  198.     windowCode=FindWindow(theEvent.where, &theWindow);
  199.     index=-1;
  200.     for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  201.         if (theWindow==gTheWindow[i])
  202.             index=i;
  203.     
  204.     switch (windowCode)
  205.     {
  206.         case inMenuBar:
  207.             AdjustMenus();
  208.             HandleMenu(MenuSelect(theEvent.where));
  209.             break;
  210.         case inContent:
  211.             if(FrontWindow() != theWindow)
  212.             {
  213.                 if (FrontWindow()==gTheWindow[kAboutMSG])
  214.                     UpdateTheWindow(kAboutMSG);
  215.                 SelectWindow(theWindow);
  216.             }
  217.             else
  218.                 switch (index)
  219.                 {
  220.                     case kAbout:
  221.                         CloseTheWindow(index);
  222.                         break;
  223.                     case kAboutMSG:
  224.                         UpdateTheWindow(index);
  225.                         Delay(30, &dummy);
  226.                         CloseTheWindow(index);
  227.                         break;
  228.                     case kHelp:
  229.                         HelpEvent();
  230.                         break;
  231.                 }
  232.             break;
  233.         case inSysWindow:
  234.             SystemClick(&theEvent, theWindow);
  235.             break;
  236.         case inDrag:
  237.             DragWindow(theWindow, theEvent.where, &gDragRect);
  238.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  239.             break;
  240.         case inGoAway:
  241.             if (TrackGoAway(theWindow, theEvent.where))
  242.             {
  243.                 gotone=FALSE;
  244.                 for (i=0; (i<NUM_WINDOWS) && (!gotone); i++)
  245.                     gotone=(theWindow==gTheWindow[i]);
  246.                     
  247.                 if (gotone)
  248.                     CloseTheWindow(i-1);
  249.                 else
  250.                     DisposeWindow(theWindow);
  251.                 
  252.                 AdjustMenus();
  253.             }
  254.             break;
  255.         case inGrow:
  256.             sizeRect = screenBits.bounds;
  257.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  258.             
  259.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  260.             if(windSize != 0)
  261.             {
  262.                 GetPort(&oldPort);
  263.                 SetPort(theWindow);
  264.                 EraseRect(&theWindow->portRect);
  265.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  266.                 InvalRect(&theWindow->portRect);
  267.                 SetPort(oldPort);
  268.             }
  269.             
  270.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  271.             
  272.             break;
  273.         case inZoomIn:
  274.         case inZoomOut:
  275.             if(TrackBox(theWindow, theEvent.where, windowCode))
  276.             {
  277.                 GetPort(&oldPort);
  278.                 SetPort(theWindow);
  279.                 ZoomWindow(theWindow, windowCode, FALSE);
  280.                 InvalRect(&theWindow->portRect);
  281.                 SetPort(oldPort);
  282.             }
  283.             
  284.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  285.             
  286.             break;
  287.     }
  288. }
  289.  
  290. void ShutDownEnvironment(void)
  291. {
  292.     ShutDownProgram();
  293.     ShutDownMSGGraphics();
  294.     ShutDownHelp();
  295. }
  296.